package com.yxt.dispatch.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxt.common.utils.ajax.PageUtils;
import com.yxt.common.utils.ajax.R;
import com.yxt.dispatch.entity.AreaEntity;
import com.yxt.dispatch.service.AreaService;
import com.yxt.dispatch.vo.SelectedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 区域表 
 *
 * @author august
 * @email 379249906@qq.com
 * @date 2020-08-13 13:43:27
 */
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = areaService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/selected")
    public R selected(){
        List<AreaEntity> entities = areaService.list(new QueryWrapper<AreaEntity>());
        List<SelectedVO> collect = entities.stream().map(attr -> {
            SelectedVO selectedVO = new SelectedVO();
            selectedVO.setLabel(attr.getAreaName());
            selectedVO.setValue(attr.getAreaId());
            return selectedVO;
        }).collect(Collectors.toList());

        return R.ok().put("data", collect);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{areaId}")
    public R info(@PathVariable("areaId") Long areaId){
		AreaEntity area = areaService.getById(areaId);

        return R.ok().put("area", area);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AreaEntity area){
		areaService.save(area);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AreaEntity area){
		areaService.updateById(area);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] areaIds){
		areaService.removeByIds(Arrays.asList(areaIds));

        return R.ok();
    }

}
